package es.udc.pfc.davidparedes.web.pages.user;

import java.util.Calendar;
import java.util.Date;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import es.udc.pfc.davidparedes.model.taskservice.TaskService;
import es.udc.pfc.davidparedes.model.userprofile.UserProfile;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;

import es.udc.pfc.davidparedes.web.pages.user.VendorSchedule;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.VENDOR)
public class AddUnavailable {
	
	@Property
	private UserProfile userProfile;
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private UserService userService;
    
    @Inject
    private TaskService taskService;

    @Property
    private Date startDate;
    
    @Property
    private Date endDate;
    
    @Property
    private String startTime;
    
    @Property
    private String endTime;
    
    @Property
    private Boolean isRange;
    
    @Component
    private Form addUnavailableForm;
    
    @Inject
    private Messages messages;
    
    
    void onActivate() {
    	try {
    		
    		userProfile = userService.findUserProfile(userSession.getUserProfileId());
  
			} catch (InstanceNotFoundException e) {
		}
    }

    
    Object onValidateFromAddUnavailableForm() {

        if (!addUnavailableForm.isValid()) {
            return this;
        }
        
        String[] unavailableStartTime = startTime.split(":");
        Integer unavailableStartHour = Integer.parseInt(unavailableStartTime[0]);
        Integer unavailableStartMin = Integer.parseInt(unavailableStartTime[1]);
        Calendar unavailableStart = Calendar.getInstance();
        unavailableStart.setTime(startDate);
        if ((0 <= unavailableStartHour) && (unavailableStartHour <= 23) && (0 <= unavailableStartMin) && (unavailableStartMin <= 59)) {
	        unavailableStart.set(Calendar.HOUR_OF_DAY, unavailableStartHour);
	        unavailableStart.set(Calendar.MINUTE, unavailableStartMin);
        } else {
        	return this;
        }
        
        String[] unavailableEndTime = endTime.split(":");
        Integer unavailableEndHour = Integer.parseInt(unavailableEndTime[0]);
        Integer unavailableEndMin = Integer.parseInt(unavailableEndTime[1]);
        Calendar unavailableEnd = Calendar.getInstance();
        unavailableEnd.setTime(endDate);
        if ((0 <= unavailableEndHour) && (unavailableEndHour <= 23) && (0 <= unavailableEndMin) && (unavailableEndMin <= 59)) {
	        unavailableEnd.set(Calendar.HOUR_OF_DAY, unavailableEndHour);
	        unavailableEnd.set(Calendar.MINUTE, unavailableEndMin);
        } else {
        	return this;
        }
        
        if (unavailableStart.before(unavailableEnd)) {
        	try {
        		if (isRange) {
        			taskService.addUnavailablesRange(unavailableStart, unavailableEnd, userProfile.getUserProfileId());
        		} else {
        			taskService.addUnavailable(unavailableStart, unavailableEnd, userProfile.getUserProfileId());
        		}
			} catch (InstanceNotFoundException e) {
				addUnavailableForm.recordError(messages.get("error-notFound"));
			}
        }
        
        return VendorSchedule.class;
        
    }

}
